package com.mall.hadoop.job.singletopicday;
import java.io.IOException;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mall.manager.service.SingleTopicDayService;
/**
 * avg(time)单题按天统计
 * @author liugb
 * date 2016 7 12
 */
public class SingletopicdayReduce extends Reducer<Text, IntWritable, Text, IntWritable>{
	public static Logger logger = LoggerFactory.getLogger(SingletopicdayReduce.class);
	@Override
	protected void reduce(Text key, Iterable<IntWritable> values,Context context) throws IOException, InterruptedException {
		// TODO Auto-generated method stub
		// 定义一行计数器
		int sum = 0;
		int count = 0;
		// 迭代他的次数
		for (IntWritable lw : values) {
			// 求和
			sum += lw.get();
			count++;
		}
		int avgtime = sum/count;
		int topic_id = Integer.parseInt(key.toString());
		SingleTopicDayService.manager_insert_singleTopicDay(topic_id, count, avgtime);
		context.write(key, new IntWritable(avgtime));
	}
		
}
